/*!
 * \file __frexpect_add.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */
#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include <frdb_cursor.h>
#include "frexp_comm.h"

#include "apmtest_comm.h"
#include "../../../src/module/syncmodule/syncmodule_core.h"

FR_STATIC Frhash_header        st_hash_expect_ct;
FR_STATIC pthread_spinlock_t   g_spin_exp_lock;

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

extern void __list_node_free_param(Frlist_node *p_st_node);

static void __frexpect_node_free(Frlist_node* p_st_node)
{
    Frexp *p_st_exp_node = NULL;

    if (p_st_node && p_st_node->data)
    {
        p_st_exp_node = (Frexp *)(p_st_node->data);
        if(p_st_exp_node->p_master_l7_module)
        {
//            __sync_fetch_and_sub(&p_st_exp_node->p_master_l7_module->ul_refcnt, 1);
            p_st_exp_node->p_master_l7_module = NULL;
        }
        if(p_st_exp_node->p_lst_hdr_param)
            frexpect_free_param(&p_st_exp_node->p_lst_hdr_param);
        frfree(p_st_exp_node);
    }
    return;
}

extern int __frexpect_add(Frexp_ref* p_st_expRef
        , u_short us_proto
        , u_int ui_src_ip
        , u_short us_src_port
        , u_int ui_dst_ip
        , u_short us_dst_port
        , u_short us_stat_src_port
        , u_short us_stat_dst_port
        , int i_l7_proto
        , Frlist_header *p_lst_hdr_param
        , Conntrack *p_st_ct);

static void __case1(void)
{
    int i_ret = 0;
    Frexp_ref *p_st_expRef = NULL;
    u_short us_proto = 7;
    u_int ui_src_ip = 16819218;
    u_short us_src_port = 8080;
    u_int ui_dst_ip = 19216811;
    u_short us_dst_port = 21;
    u_short us_stat_src_port = 127;
    u_short us_stat_dst_port = 140;
    int i_l7_proto = 7;
    Frlist_header *p_lst_hdr_param = NULL;
    Conntrack *p_st_ct = NULL;

    frhash2_create(&st_hash_expect_ct, 128);
    pthread_spin_init(&g_spin_exp_lock, 0);

    p_st_expRef = (Frexp_ref *)frmalloc (sizeof (Frexp_ref));
    p_st_expRef->p_hash_hdr = &st_hash_expect_ct;
    p_st_expRef->p_splock = &g_spin_exp_lock;

    apmtest_create_ct(&p_st_ct, NULL, (char *)"192.168.1.1", (char *)"192.168.1.2", 80, 21);

    i_ret = __frexpect_add(p_st_expRef, us_proto, ui_src_ip, us_src_port, ui_dst_ip, us_dst_port, us_stat_src_port, us_stat_dst_port, i_l7_proto, p_lst_hdr_param, p_st_ct);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    apmtest_destroy_ct(&p_st_ct);

    pthread_spin_init(p_st_expRef->p_splock, 0);
    frhash2_free(p_st_expRef->p_hash_hdr, __frexpect_node_free);
    pthread_spin_destroy(p_st_expRef->p_splock);
    frfree(p_st_expRef);
    return;

}


static void __case2(void)
{
    int i_ret = 0;
    Frexp_ref* p_st_expRef = NULL;
    u_short us_proto = 7;
    u_int ui_src_ip = 16819218;
    u_short us_src_port = 8080;
    u_int ui_dst_ip = 19216811;
    u_short us_dst_port = 21;
    u_short us_stat_src_port = 127;
    u_short us_stat_dst_port = 140;
    int i_l7_proto = 7;
    Frlist_header *p_lst_hdr_param = NULL;
    Conntrack *p_st_ct = NULL;

    frhash2_create(&st_hash_expect_ct, 128);
    pthread_spin_init(&g_spin_exp_lock, 0);

    apmtest_create_ct(&p_st_ct, NULL, (char *)"192.168.1.1", (char *)"192.168.1.2", 80, 21);

    i_ret = __frexpect_add(p_st_expRef, us_proto, ui_src_ip, us_src_port, ui_dst_ip, us_dst_port, us_stat_src_port, us_stat_dst_port, i_l7_proto, p_lst_hdr_param, p_st_ct);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);
    apmtest_destroy_ct(&p_st_ct);
    pthread_spin_destroy(&g_spin_exp_lock);
    frhash2_free(&st_hash_expect_ct, __frexpect_node_free);
    return;

}


int add_suite___frexpect_add(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("frdb_cursor __frexpect_add()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);

    return FR_SUCC;
}
